extends ../../layout

block content
    .container
        .row
            mixin inputs
                if movie._id
                    input(type='hidden', name='id', value='#{movie._id}')
                else
                    .form-group
                        label.col-sm-2.control-label(for='doubanMovieId') 豆瓣电影ID
                        .col-sm-4
                            input#doubanMovieId.form-control(type='text', placeholder='豆瓣电影ID')
                .form-group
                    label.col-sm-2.control-label(for='movieName') 电影名称
                    .col-sm-10
                        input#movieName.form-control(type='text', name='name', placeholder='电影名称', value='#{movie.name}')
                .form-group
                    label.col-sm-2.control-label(for='movieDirector') 导演
                    .col-sm-10
                        input#movieDirector.form-control(type='text', name='director', placeholder='导演', value='#{movie.director}')
                .form-group
                    label.col-sm-2.control-label(for='movieYear') 上映时间
                    .col-sm-10
                        input#movieYear.form-control(type='text', name='year', placeholder='上映时间', value='#{movie.year}')
                .form-group
                    label.col-sm-2.control-label(for='moviePoster') 海报地址
                    .col-sm-10
                        input#moviePoster.form-control(type='text', name='poster', placeholder='海报地址', value='#{movie.poster}')
                .form-group
                    label.col-sm-2.control-label(for='moviePlay') 播放地址
                    .col-sm-10
                        input#moviePlay.form-control(type='text', name='play', placeholder='播放地址', value='#{movie.play}')
                .form-group
                    label.col-sm-2.control-label(for='movieSummary') 简介
                    .col-sm-10
                        input#movieSummary.form-control(type='text', name='summary', placeholder='简介', value='#{movie.summary}')
                .form-group
                    .col-sm-offset-2.col-sm-10
                        each category in categories
                            label.checkbox-inline
                                input(type='checkbox', name='categories', value='#{category._id}')
                                | #{category.name}
                .form-group
                    .col-sm-offset-2.col-sm-10
                        button.btn.btn-default(type='submit') 提交

            if movie._id
                form.form-horizontal(method='post', action='/movie/update')
                    +inputs
                script.
                    var cateInMovie = '#{cateInMovie}';
                    var cateInMovieArr = cateInMovie.split(',');
                    $('input[type="checkbox"]').each(function () {
                        var id = $(this).val();
                        if ($.inArray(id, cateInMovieArr) > -1) {
                          $(this).attr('checked', true);
                        }
                    });
                    $('input[type="checkbox"]').on('click', function () {
                        var cateId = $(this).val();
                        var checked = $(this).is(':checked');
                        var movieId = $('input[name="id"]').val();
                        var data = {
                            cateId: cateId,
                            add: checked,
                            movieId: movieId
                        };
                        $.post('/category/updateMovies', data);
                    });
            else
                form.form-horizontal(method='post', action='/movie/create')
                    +inputs

    script(src='/js/doubanApi.js', type='text/javascript')